package com.springboot.demo.util;

import java.util.Random;

/**
 * @Author: J.K
 * @Date: 2021-09-10 13:34
 * @Description: 生成6位随机数-短信验证码
 */
public class RandomUtil {
    public static String getRandomNum() {
        Random random = new Random();
        String randomNum = random.nextInt(100000) + "";
        if (randomNum.length() != 6) {
            System.out.println("6位伪随机数：" + randomNum);
            return getRandomNum();
        }
        System.out.println("6位随机数：" + randomNum);
        return randomNum;
    }

    /**
     * 生成6位随机数
     *
     * @return
     */
    public static String randomNum() {
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder(4);
        for (int i = 0; i < 4; i++) {
            char ch = str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        System.out.println(sb.toString());
        String code = sb.toString();
        return code;
    }
}
